package game.gui;

import java.awt.*;
import java.awt.event.*;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.swing.*;
import javax.swing.border.EmptyBorder;

import game.model.*;

/**
 * @author Aqilah
 * Class MainFrame displays the main GUI of the game
 */
public class MainFrame extends JFrame implements ActionListener{
	private JMenu menu;
	private JMenuItem menuItemNewGame;
	private JMenuItem menuItemSettings;
	private JMenuItem menuItemAbout;
	private JMenuItem menuItemExit;	
	private Player player; // the player
	private Board board; // the game board
	private Game game; // the game
	private JPanel pnlBoard;
	private JLabel lblStatus;
	
	private Block firstBlock; // first selected block
	private boolean isUpdating = false; // used for animation
	
	private final String SAVE_FILE_NAME = "scores.txt";
	
	/** Constructor */
	public MainFrame(){
		super("Mirror Mirror");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		createGUI();

		game = new Game();
		pnlBoard.setBackground(game.getBackground());
		
		// set default locale as US
		setLocale(new Locale("en", "US"));		
	}
	
	/** Create the GUI */
	public void createGUI(){
		setLayout(new BorderLayout());
		
		JMenuBar menuBar = new JMenuBar();
		menu = new JMenu("Menu");
		menu.setMnemonic(KeyEvent.VK_M);
		menuBar.add(menu);
		
		// new game
		menuItemNewGame = new JMenuItem("New Game", KeyEvent.VK_N);
		menuItemNewGame.addActionListener(this);
		menu.add(menuItemNewGame);
		
		// settings
		menuItemSettings = new JMenuItem("Settings", KeyEvent.VK_S);
		menuItemSettings.addActionListener(this);
		menu.add(menuItemSettings);
		
		// about
		menuItemAbout = new JMenuItem("About", KeyEvent.VK_A);
		menuItemAbout.addActionListener(this);
		menu.add(menuItemAbout);
		
		// add separator
		menu.addSeparator();
		
		// exit
		menuItemExit = new JMenuItem("Exit", KeyEvent.VK_X);
		menuItemExit.addActionListener(this);
		menu.add(menuItemExit);
		
		setJMenuBar(menuBar);
		
		pnlBoard = new JPanel();		
		pnlBoard.setBorder(new EmptyBorder(20, 20, 20, 20) );
		add(pnlBoard, BorderLayout.CENTER);
		
		lblStatus = new JLabel("Name:       Score:   ");
		add(lblStatus, BorderLayout.SOUTH);
		lblStatus.setVisible(false);
		
		setSize(800, 600);		
	}
	
	/** Update language of the game */
	private void updateLanguage(){
		if (game.getLanguage().equals(Game.ENGLISH))
			setLocale(new Locale("en", "US"));
		else if (game.getLanguage().equals(Game.FRENCH))
			setLocale(new Locale("fr", "FR"));
		else if (game.getLanguage().equals(Game.SPANISH))
			setLocale(new Locale("es", "ES"));
		
		// update labels
		ResourceBundle rb = ResourceBundle.getBundle("languages.MessagesBundle", getLocale());
		menu.setText(rb.getString("menu"));
		menuItemNewGame.setText(rb.getString("newgame"));
		menuItemSettings.setText(rb.getString("settings"));
		menuItemAbout.setText(rb.getString("about"));
		menuItemExit.setText(rb.getString("exit"));		
	}
	
	
	/** Show settings dialog and apply settings if user clicks OK */
	public void settings(){
		SettingsDialog dlg = new SettingsDialog(this);		
		dlg.setVisible(true);
		if (dlg.language != null){
			game.setBoardSize(dlg.boardSize);
			game.setGameMode(dlg.gameMode);
			game.setLanguage(dlg.language);
			game.setBackground(dlg.color);
			pnlBoard.setBackground(game.getBackground());
			updateLanguage();
			pnlBoard.revalidate();
			this.repaint();
		}
	}
	
	/** Show About dialog */
	public void about(){
		new AboutDialog(this).setVisible(true);
	}
	
	/** Exit the application */
	public void exit(){
		System.exit(0);
	}
	
	/** Update status and the game board */
	public void update(){		
		
	}
	
	
	/** Handle click at row, col */
	public void handleClick(int row, int col){
		if (isUpdating)
			return;
		isUpdating = true;
		UpdateThread thread = new UpdateThread(row, col);
		thread.start();
	}

	/** Implements ActionListener interface */
	public void actionPerformed(ActionEvent arg0) {
		Object button = arg0.getSource();
		if (button == menuItemNewGame)
			//newGame();
		//else if (button == menuItemSettings)
			settings();
		else if (button == menuItemAbout)
			about();
		else if (button == menuItemExit)
			exit();
	}
	
	
	/** Class UpdateThread updates the UI and provides simple animation */
	private class UpdateThread extends Thread{
		private int row, col;
		public UpdateThread(int row, int col){
			this.row = row;
			this.col = col;
			this.col = col;
		}
	}
}
